package com.legoo.studaychineseonline.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;


import com.legoo.studaychineseonline.R;
import com.legoo.studaychineseonline.base.BaseActivity;
import com.legoo.studaychineseonline.utils.IsNetWork;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
*初始界面
 * */

public class SplashActivity extends BaseActivity {



    @BindView(R.id.ll_splash)
    LinearLayout llSplash;

    @Override
    protected void myOnCreate(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
    }



    @Override
    protected void resolveBundle(Bundle bundle) {

    }

    @Override
    protected int myLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initData() {

        llSplash.setVisibility(View.GONE);

        if (!IsNetWork.isNetworkAvailable(this)){

            llSplash.setVisibility(View.VISIBLE);
            startAnim();

        }else {



            //判断用户是否已经授权
            if (ContextCompat.checkSelfPermission(SplashActivity.this, "Manifest.permission.ACCESS_FINE_LOCATION") != PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(SplashActivity.this, "Manifest.permission.ACCESS_COARSE_LOCATION") != PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                //注意第二个参数没有双引号
                ActivityCompat.requestPermissions(SplashActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                }, 1);

            } else {
                llSplash.setVisibility(View.VISIBLE);
                startAnim();

            }

        }
    }

    /**
     * 启动动画
     */

    private void startAnim() {
        // 渐变动画,从完全透明到完全不透明
        AlphaAnimation alpha = new AlphaAnimation(0, 1);
        // 持续时间 2 秒
        alpha.setDuration(1500);
        // 动画结束后，保持动画状态
        alpha.setFillAfter(true);

        // 设置动画监听器
        alpha.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            // 动画结束时回调此方法
            @Override
            public void onAnimationEnd(Animation animation) {
                // 跳转到下一个页面
                startIntent(MainActivity.class);
                finish();

            }
        });

        // 启动动画
        llSplash.startAnimation(alpha);
    }






    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 原来的敏感操作代码：发短信或者收短信
                    llSplash.setVisibility(View.VISIBLE);
                    startAnim();


                } else {
                    showToast("您拒绝了权限,可能导致某些功能无法使用");
                    llSplash.setVisibility(View.VISIBLE);
                    startAnim();
                }
                break;
        }

    }


}
